package com.csu.cphone.service.impl;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.commons.fileupload.FileItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.csu.common.constants.CommonParams;
import com.csu.common.utils.FileUtil;
import com.csu.cphone.dao.PhoneBrandMapper;
import com.csu.cphone.pojo.PhoneBrandPojo;
import com.csu.cphone.service.PhoneService;

/**
 * @项目名称：cphone
 * @类名称：PhoneServiceImpl
 * @类描述：
 * @创建人：Tangc
 * @创建时间：2017年9月14日 下午2:58:48
 * @company:
 */
@Service
public class PhoneServiceImpl implements PhoneService
{

    @Autowired
    private PhoneBrandMapper phoneBrandMapper;



    /**
     * @see com.csu.cphone.service.PhoneService#wirteFile(org.springframework.web.multipart.commons.CommonsMultipartFile)
     */
    @Override
    public String wirteFile(CommonsMultipartFile productFile)
    {
        String requestUrl = null;
        if (productFile.getSize() == 0 || productFile == null)
        {
            requestUrl = "";
        }
        else
        {
            FileItem fileItem = productFile.getFileItem();
            String fileName = fileItem.getName();

            try
            {
                fileName = new String(fileName.getBytes(), "UTF-8");
            }
            catch (UnsupportedEncodingException e1)
            {
                e1.printStackTrace();
            }

            String folder = "/phone";
            fileName = System.currentTimeMillis() + "." + fileName;
            requestUrl = CommonParams.REQUEST_URL + folder + "/" + fileName;
            try
            {

                FileUtil.writeFile(productFile, fileName, folder);
            }
            catch (FileNotFoundException e)
            {
                e.printStackTrace();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
        return requestUrl;
    }

    /**
     * @see com.csu.cphone.service.PhoneService#addBrand(java.lang.String,
     *      java.lang.String)
     */
    @Override
    public PhoneBrandPojo addBrand(String brandName, String url)
    {
        PhoneBrandPojo phoneBrand = new PhoneBrandPojo();
        phoneBrand.setPicUrl(url);
        phoneBrand.setBrandName(brandName);
        phoneBrandMapper.insert(phoneBrand);
        return phoneBrand;
    }

   

    /**
     * @see com.csu.cphone.service.PhoneService#getBrandList()
     */
    @Override
    public List<PhoneBrandPojo> getBrandList()
    {
        return phoneBrandMapper.selectBrandList();
    }


    

}
